package com.jtny.nytb.config.varible.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.config.excel.biz.ImportExcelBiz;
import com.jtny.nytb.config.excel.util.EasyExcelUtils;
import com.jtny.nytb.config.varible.service.ConfigGlobalManagementBiz;
import com.jtny.nytb.config.varible.vo.ConfigGlobalManagementFormVO;
import com.jtny.nytb.config.varible.vo.ConfigGlobalManagementQueryVO;
import com.jtny.nytb.config.varible.vo.ConfigGlobalManagementVO;
import com.jtny.nytb.config.varible.vo.ExcelGlobalParamVO;
import com.jtny.nytb.valid.param.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 全局变量管理Controller类
 *
 * @author 智慧能源中心 - 伍天宇
 * @package com.jtny.nytb.config.generic.variable.controller
 * @fileName ConfigGlobalManagementController.java
 * @createTime 2023年09月05日 09:38
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@RestController
@ResponseBody
@Api(value = "全局变量管理", tags = "全局变量管理")
@ApiSort(202303)
@RequestMapping("/generic/global")
@AllArgsConstructor
public class ConfigGlobalManagementController {

    private ConfigGlobalManagementBiz configGlobalManagementBiz;
    private ImportExcelBiz importExcelBiz;

    @ApiOperation("分页查询全局变量")
    @ApiOperationSupport(order = 1)
    @PostMapping("/query")
    public WebResponse<PageData<ConfigGlobalManagementVO>> queryPage(@RequestBody ConfigGlobalManagementQueryVO configGlobalManagementQueryVO) {
        PageData<ConfigGlobalManagementVO> result = configGlobalManagementBiz.queryPage(configGlobalManagementQueryVO);
        return WebResponse.success(result);
    }

    @ApiOperation("保存全局变量")
    @ApiOperationSupport(order = 2)
    @PostMapping("/save")
    public WebResponse<?> save(@Validated @RequestBody ConfigGlobalManagementFormVO configGlobalManagementFormVO) {
        configGlobalManagementBiz.save(configGlobalManagementFormVO);
        return WebResponse.success("操作成功");
    }

    @ApiOperation("编辑全局变量")
    @ApiOperationSupport(order = 3)
    @PutMapping("/update")
    public WebResponse<?> update(@Validated(Update.class) @RequestBody ConfigGlobalManagementFormVO configGlobalManagementFormVO) {
        configGlobalManagementBiz.update(configGlobalManagementFormVO);
        return WebResponse.success("操作成功");
    }

    @ApiOperation("详情全局变量")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, paramType = "path")
    @ApiOperationSupport(order = 4)
    @GetMapping("detail/{id}")
    public WebResponse<ConfigGlobalManagementVO> detail(@PathVariable("id") Long id) {
        ConfigGlobalManagementVO result = configGlobalManagementBiz.detail(id);
        return WebResponse.success(result);
    }


    @ApiOperation("删除全局变量")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, paramType = "path")
    @ApiOperationSupport(order = 5)
    @DeleteMapping("/delete/{id}")
    public WebResponse<?> delete(@PathVariable("id") Long id) {
        configGlobalManagementBiz.deleteById(id);
        return WebResponse.success("操作成功");
    }


    @PostMapping("/export-template")
    @ApiOperation(value = "获取导入模板")
    public void exportTemplate(HttpServletResponse response) {
        EasyExcelUtils.exportExcel(response, "全局变量导入模版", null, ExcelGlobalParamVO.class);
    }

    @ApiOperation("导入全局变量")
    @ApiImplicitParam(name = "file", value = "全局变量excel文件", required = true, paramType = "query", dataType = "MultipartFile")
    @ApiOperationSupport(order = 9)
    @PostMapping("/import")
    @ResponseBody
    public WebResponse<?> importGlobalValue(@RequestParam(value = "file") MultipartFile file) {
        List<ExcelGlobalParamVO> list = EasyExcelUtils.importExcel(file, ExcelGlobalParamVO.class);
        importExcelBiz.importGlobalValueExcel(list);
        return WebResponse.success();
    }

    @ApiOperation("导出全局变量")
    @ApiOperationSupport(order = 10)
    @PostMapping("/export")
    @ApiImplicitParam(name = "idList", value = "id列表", required = true, paramType = "body", dataType = "List<Long>")
    @ResponseBody
    public void exportGlobalValue(HttpServletResponse response, @RequestBody List<Long> idList) {
        ConfigGlobalManagementQueryVO paramVO = new ConfigGlobalManagementQueryVO();
        paramVO.setIdList(idList);
        paramVO.setPageSize(10000);
        PageData<ConfigGlobalManagementVO> page = configGlobalManagementBiz.queryPage(paramVO);
        List<ExcelGlobalParamVO> toList = BeanUtils.copyToList(page.getData(), ExcelGlobalParamVO.class);
        EasyExcelUtils.exportExcel(response, "数据字典", toList, ExcelGlobalParamVO.class);
    }
}
